freeslot("sfx_hitsnd")

addHook("MobjDamage", function(mo, i, s, d, dt)
	if not (mo.flags & (MF_ENEMY|MF_BOSS)) then return end
	if mo.type == MT_PLAYER then return end //for if you gave the player an enemy/boss flag for some reason.
	if mo.info.specialdamagehook then return end
	if RFW.SPECIALDAMAGEHOOK[mo.type] then return end //allow modders to hijack the damage hook if need be.
	//if mo.health <= 0 then return end //to stop us from killing enemies twice
	local damage = d
	if (i or s) then
		if (i and i.rfwdamage ~= nil) or (s and s.rfwdamage ~= nil) then //do it this way
			damage = (i and i.rfwdamage) or (s and s.rfwdamage)
		//else //this is fallback for the globals table
		//	damage = P_RandomRange(((RFW.DAMAGETABLE[i and i.type or s and s.type]) and RFW.DAMAGETABLE[i and i.type or s and s.type][1] or RFW.DefaultDamage[1]), (((RFW.DAMAGETABLE[i and i.type or s and s.type]) and RFW.DAMAGETABLE[i and i.type or s and s.type][2] or RFW.DefaultDamage[2])))
		end
	end
	/*if s and s.type == MT_PLAYER then
		S_StartSound(s, sfx_hitsnd, s.player)
	end*/
	if not damage then return end
	//print(damage)
	mo.health = $ - damage
	RFW.Bleed(mo)
	if mo.health <= 0 then
		//P_KillMobj(mo, i, s, dt) //stop, double kill bug
	else
		if s then
			mo.target = s //start attacking the guy who just hit us, maybe I'll eventually add the reaction time based target switching from doom but for right now this is fine enough.
			if mo.state == mo.info.spawnstate and mo.info.seestate ~= S_NULL then
				mo.state = mo.info.seestate
			end
		end
		local bruh
		if mo.info.rfwpainchance ~= nil then
			bruh = mo.info.rfwpainchance
		else
			bruh = 256
		end
		if P_RandomRange(1, 256) <= bruh then //0 is never pain chance, 256 is always painchance
			mo.state = mo.info.painstate
		end
		return true
	end
end)

function RFW.DirectDamageEnemy(mo, i, s, d, dt) //it should be safe to use MobjDamage in pretty much any case you normally need for lua modding, especially for hitscans, but you never can be too sure so you can use this just incase.
	if not (mo.flags & (MF_ENEMY|MF_BOSS)) then
		P_DamageMobj(mo, i, s, d, dt)
		return
	end
	if mo.type == MT_PLAYER then return end //for if you gave the player an enemy/boss flag for some reason.
	if mo.info.specialdamagehook then return end
	if RFW.SPECIALDAMAGEHOOK[mo.type] then return end //allow modders to hijack the damage hook if need be.
	//if mo.health <= 0 then return end //to stop us from killing enemies twice
	/*if s and s.type == MT_PLAYER then
		S_StartSound(s, sfx_hitsnd, s.player)
	end*/
	mo.health = $ - d
	RFW.Bleed(mo)
	if mo.health <= 0 then
		P_KillMobj(mo, i, s, dt) //stop, double kill bug
	else
		if s then
			mo.target = s //start attacking the guy who just hit us, maybe I'll eventually add the reaction time based target switching from doom but for right now this is fine enough.
			if mo.state == mo.info.spawnstate and mo.info.seestate ~= S_NULL then
				mo.state = mo.info.seestate
			end
		end
		local bruh
		if mo.info.rfwpainchance ~= nil then
			bruh = mo.info.rfwpainchance
		else
			bruh = 256
		end
		if P_RandomRange(1, 256) <= bruh then //0 is never pain chance, 256 is always painchance
			mo.state = mo.info.painstate
		end
		return true
	end
end